package buco.integrity.typehierarchy;

import org.json.simple.*;

import buco.core.api.SemanticConstraint;

public class DefineInheritance extends SemanticConstraint {

	private ClassGraph graph; 
	
	public DefineInheritance() { 
		graph = new ClassGraph(); 
	}
	
	public ClassGraph getClassGraph() { 
		return graph; 
	}
	
	@Override
	protected void deserialize(JSONObject jsonObject) throws Exception {
		Object objClasses = jsonObject.get("baseClasses"); 
		if(objClasses == null) 
			return; 
		
		JSONArray arr = (JSONArray) objClasses; 
		for(int i=0; i < arr.size(); i++) {
			JSONObject clazzObject = (JSONObject) arr.get(i);
			insert(clazzObject);
		}
	}
	
	private void insert(JSONObject classObject) { 
		String className = classObject.get("class").toString(); 
		JSONArray subClasses = (JSONArray) classObject.get("subClasses");
		
		for(int i=0; i < subClasses.size(); i++) { 
			graph.insert(className, subClasses.get(i).toString()); 
		}
	}

}
